package com.soft.line.controller;

import com.soft.line.entity.ContractInfo;
import com.soft.line.service.CodeDetailService;
import com.soft.line.service.ContractInfoService;
import com.soft.line.util.exception.CommonResult;
import com.soft.line.util.jackson.converter.StringToDateUtil;
import com.soft.line.util.poi.PoiUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Description 合同相关
 * @author wu.xueqiao on 2020/5/21.
 * @version 0.1
 */
@Api(description = "合同接口")
@Slf4j
@RequestMapping("/dataManage")
@RestController
public class ContractInfoController {

    @Autowired
    private ContractInfoService contractInfoService;
    @Autowired
    private CodeDetailService codeDetailService;

    @PostMapping("/getAllAboutContracts")
    public CommonResult getAllAboutContracts(@RequestBody Map<String,Object> map){
        return CommonResult.success(contractInfoService.getAllAboutContracts(map));
    }

    @ApiOperation(value = "获取所有合同", notes = "获取所有合同")
    @PostMapping("/contracts")
    public CommonResult getAllContracts(@RequestBody Map<String,Object> map){
        return CommonResult.success(contractInfoService.getAllContracts(map));
    }

    @ApiOperation(value = "删除合同", notes = "删除合同")
    @DeleteMapping("/contract")
    public CommonResult deleteContract(@RequestBody Map<String,Object> map){
        contractInfoService.delete(map);
        return CommonResult.success("删除成功");
    }

    @ApiOperation(value = "保存合同", notes = "保存合同")
    @PostMapping("/contract")
    public CommonResult saveContract(@RequestBody @Validated ContractInfo contractInfo){
        if (contractInfoService.countContractByIdAndName(contractInfo.getId(),contractInfo.getContractName())>0){
            return CommonResult.success("存在相同合同名称","存在相同合同名称");
        }
        if (contractInfo.getBeginDate() != null && contractInfo.getEndDate() != null) {
            if (contractInfo.getBeginDate().after(contractInfo.getEndDate())) {
                return CommonResult.success("起始日期不能>终止日期", "起始日期不能>终止日期");
            }
        }
        if (contractInfo.getId()==null){
            //添加
            contractInfoService.add(contractInfo);
            return CommonResult.success("添加成功");
        }else{
            //修改
            contractInfoService.update(contractInfo);
            return CommonResult.success("修改成功");
        }
    }

    @ApiOperation(value = "导入合同", notes = "导入合同")
    @PostMapping("/contract/import")
    public CommonResult importContract(@RequestParam MultipartFile file, HttpServletRequest request) {
        if (file == null){
            return CommonResult.success("上传文件不能为空","上传文件不能为空");
        }
        String fileName = file.getOriginalFilename();
        if (!fileName.matches("^.+\\.(?i)(xls)$") && !fileName.matches("^.+\\.(?i)(xlsx)$")) {
            return CommonResult.success("上传文件格式错误，请上传后缀为.xls或.xlsx的文件","上传文件格式错误，请上传后缀为.xls或.xlsx的文件");
        }
        List<ContractInfo> contractInfoList = null;
        Map<String,Object> map = PoiUtils.excel2ContractInfo(file,codeDetailService,contractInfoService);
        String warningMsg = (String)map.get("warningMsg");
        if (warningMsg.length()>0){
            //System.out.println("warningMsg:"+warningMsg);
            return CommonResult.failed(warningMsg);
        }else{
            contractInfoList = (List<ContractInfo>)map.get("contractInfoList");
            contractInfoService.insertList(contractInfoList);
            return CommonResult.success("导入成功");
        }
    }

    @ApiOperation(value = "导出合同", notes = "导出合同")
    @PostMapping("/contract/export")
    public void exportContract(@RequestBody Map<String, Object> map, HttpServletResponse response) {
        List<ContractInfo> contractInfoList = contractInfoService.getAllCNByIDList(map);
        PoiUtils.contract2Excel(contractInfoList,response);
    }

}
